﻿#_*_ coding: UTF-8 _*_

from core import uicomponents, db
from PyQt4 import QtGui, QtCore
from servers.rootsLike.definitions import *
from servers.quran.quran_window import *
import servers.quran.ui

def showSimilarAyat():
	uicomponents.SharedUi.busyDialog.tellBusy(u'</font><font size=12 face="B Nazanin">صبر کنيد</font>')
	aye = uicomponents.SharedUi.selectionStartWord.aye	
	s = ""
	for word in aye.words:
		s = s + "'%s':%d, 	" % (word.noErabWord,1) 
	o = QtGui.QInputDialog.getText(None,u'مقدار اهمين هرکدام از کلمات را انتخاب کنيد',u'عدد اهميت هر کلمه را ميتوانيد تغيير دهيد.',QtGui.QLineEdit.Normal,QtCore.QString.fromUtf8(s))
	
	if not o[1]:
		return None
	oo = QtGui.QInputDialog.getInteger(None,u'تعداد نتايج',u'تعداد نتايج',20)
	if not oo[1]:
		return None
	limit = oo[0]
	weights = eval("{%s '':0}" % o[0])
	print weights
	root_index = {}
	for word in aye.words:
		if word.noErabWord in weights:
			for root in word.roots:
				if root.root.id in root_index:
					root_index[root.root.id] = root_index[root.root.id] + (weights[word.noErabWord]+0.001) / len(word.roots)
				else:
					root_index[root.root.id] = (weights[word.noErabWord]+0.001) / len(word.roots)
					

	ayat = []
	result = findSimilarAyat(index_dic=root_index)
	t = result.keys()
	for r in t:
		if r > 1:
			ayat = ayat + [[b,[]] for b in result[r]]
			if len(ayat)>=limit:
				break;
	uicomponents.SharedUi.busyDialog.tellDone()
	servers.quran.ui.quranSearchResult.showAyat(ayat)

showSimilarAyatAction = QtGui.QAction(u'يافتن آيات مشابه', None)
QtCore.QObject.connect(showSimilarAyatAction, QtCore.SIGNAL('triggered()'), showSimilarAyat)


uicomponents.SharedUi.wordPopupMenu.addAction(showSimilarAyatAction)
uicomponents.SharedUi.ayePopupMenu.addAction(showSimilarAyatAction)
uicomponents.SharedUi.modulesListMenu.addAction(showSimilarAyatAction)
